//
//  ViewController.m
//  Hyphenate
//
//  Created by cptech on 2017/11/13.
//  Copyright © 2017年 cptech_yj. All rights reserved.
//

#import "ViewController.h"
#import "LinkExportClient.h"
#import "LinkerChatViewController.h"

@interface ViewController () <LKChatManagerDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
   
    LinkExportClient *client = [LinkExportClient sharedClient];
    [client openConnect];
    if(!client.isConnected){
        return;
    }
    
    
    
//    //构建消息
//    LKTextMessageBody *body = [[LKTextMessageBody alloc] initWithText:@"hello_12345"];
//    LKMessage *message = [[LKMessage alloc] initWithConversationID:@"uuid_test" from:@"38956" to:@"38956" body:body ext:@{}];
//    //发送消息
//    [client.chatManager sendMessage:message progress:nil completion:^(LKMessage *message, LKError *error) {
//
//    }];
    
    
    
//    [client logoutCompletion:^(LKError *aError) {
//        if(!aError){
//            NSLog(@"退出登录成功");
//        }
//        else{
//            NSLog(@"%@",aError);
//        }
//    }];
}

- (void)dealloc{
    
    [[LinkExportClient sharedClient] closeConnect];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)connect:(id)sender{

    LinkExportClient *client = [LinkExportClient sharedClient];
    if(!client.isConnected){
        NSLog(@"链接构建不成功");
        return;
    }
    //登陆
    [client loginWithUsername:@"38956" password:@"c62c66585e1ba998326c0bfd2cccc6aa" completion:^(NSString *aUsername, LKError *aError) {

        if(!aError){
            NSLog(@"登录成功");
            LKConversation *conversationModel = [[LKConversation alloc] init];
            
            LinkerChatViewController *chatController = [[LinkerChatViewController alloc] initWithConversationChatter:@"12345" conversationType:LKConversationTypeChat];
            chatController.title = @"xixi";
            chatController.hidesBottomBarWhenPushed=YES;
            UserInfo *userInfo = [[UserInfo alloc] init];
            userInfo.userID = 38956;
            
            [self.navigationController pushViewController:chatController animated:YES];
            [chatController setChatFirend:userInfo isSave:NO];
        }
        else{
            NSLog(@"%@",aError);

        }
    }];

    
    
    //添加事件回调
    [client.chatManager addDelegate:self];
    
}

#pragma mark  LKChatManagerDelegate
- (void)messagesDidReceive:(LKMessage *)aMessages{
    LKTextMessageBody *body = (LKTextMessageBody *)aMessages.body;
    NSLog(@"收到消息%@",body.text);
}

@end
